const tree = [
  {
    title: '0-0',
    key: '0-0',
    children: [
      {
        title: '0-0-0',
        key: '0-0-0',
        children: [{ title: '0-0-0-0', key: '0-0-0-0' }]
      },
      {
        title: '0-0-1',
        key: '0-0-1'
      }
    ]
  }
]
/**
 * 深度优先遍历（DFS）扁平化树结构
 * @param {*} source 树结构数据
 * @param {*} childName 子节点属性key值，默认为children
 * @param {*} nodeElName 节点属性key值，默认为id
 * @returns {allNodes: [], allIds: []} 所有去除 childrens节点对象集合，所有节点id集合
 */

function flatTree(source, childName = 'children', nodeElName = 'id') {
  const allNodes = [] // 所有去除childrens节点对象集合
  const dfs = array => {
    array.forEach(el => {
      // const cloneEl = cloneDeep(el);
      const cloneEl = JSON.parse(JSON.stringify(el))
      delete cloneEl[childName]
      allNodes.push(cloneEl)
      if (el[childName] instanceof Array && el[childName].length) {
        dfs(el[childName])
      }
    })
  }
  dfs(source)
  const uniqIds = allNodes.map(el => el[nodeElName])
  return { allNodes, allIds: uniqIds }
}
const result = flatTree(tree, 'children', 'key')
console.log(result)
